package com.example.controller;

import com.example.anno.Log;
import com.example.pojo.Dept;
import com.example.pojo.Result;
import com.example.service.DeptService;
import com.example.service.impl.DeptServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: Sun
 * @Date: 2025/9/10 - 09 - 10 - 16:31
 * @Description: com.example.controller
 */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
//    private static final Logger log = LoggerFactory.getLogger(DeptController.class);//固定
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result list() {
//        System.out.println("查询全部部门的数据");
        log.info("查询全部部门的数据");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    //方式一:基于HttpServletRequest（不推荐）
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request) {
//        String sid = request.getParameter("id");
//        int id = Integer.parseInt(sid);
//        System.out.println("根据id删除部门的数据：" + id);
//        return Result.success();
//    }
//方式二:基于@RequestParam
//注意事项：一旦声明了@RequestParam，该参数在请求时必须传递，如果不传递就会报错（默认required为true）
//    @DeleteMapping("/depts")
//    public Result delete(@RequestParam(value = "id",required = false) Integer id) {
//        System.out.println("根据id删除部门：" + id);
//        return Result.success();
//    }
//方式三：省略了RequestParam注解
    @Log
    @DeleteMapping
    public Result delete(Integer id) {
//        System.out.println("根据id删除部门：" + id);
        log.info("根据id删除部门：{}", id);
        deptService.deleteById(id);

        return Result.success();
    }
@Log
    @PostMapping
    public Result add(@RequestBody Dept dept) {
//        System.out.println("添加部门：" + dept);
        log.info("添加部门：{}", dept);
        deptService.add(dept);
        return Result.success();
    }

    //    @GetMapping("/depts/{id}")
//    public Result getInfo(@PathVariable("id") Integer deptId){
//        System.out.println("根据id查询部门的数据：" + deptId);
//        return Result.success();
//    }
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
//        System.out.println("根据id查询部门的数据：" + id);
        log.info("根据id查询部门的数据：{}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept) {
//        System.out.println("修改部门：" + dept);
        log.info("修改部门：{}", dept);
        deptService.update(dept);
        return Result.success();

    }
}


